﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmTaxInfo : Form
    {
        DialogResult dlgResult;
        int _TaxID;
        TaxInfo _TaxInfo;
        TaxClient _wcfTax;
        RoleRightInfo _listRole;
        bool isChange = false;
        public frmTaxInfo(int TaxID, RoleRightInfo listRole)
        {
            InitializeComponent();
            LoadLanguage();
            _TaxInfo = new TaxInfo();
            _wcfTax = Service.Tax();
            dlgResult = DialogResult.Cancel;
            _TaxID = TaxID;
            this.KeyPreview = true;
            if (_TaxID > 0)
            {
                _TaxInfo = _wcfTax.Tax_GetInfo(_TaxID);
                Info2Form();
            }
            _listRole = listRole;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        private void Info2Form()
        {
            txtBottom.Text = _TaxInfo.Bottom.ToString("#,##0");
            txtTop.Text = _TaxInfo.Top.ToString("#,##0");
            txtPercent.Text = _TaxInfo.Percent.ToString("#0");
            isChange = false;
        }

        private void Form2Info()
        {
            _TaxInfo.ID = _TaxID;
            _TaxInfo.Bottom = Convert.ToDecimal(txtBottom.Text);
            _TaxInfo.Top = Convert.ToDecimal(txtTop.Text);
            _TaxInfo.Percent = Convert.ToDecimal(txtPercent.Text);
            _TaxInfo.Value = _TaxInfo.Percent / 100;
        }

        bool checkValid()
        {
            decimal _dec;
            if (!decimal.TryParse(txtBottom.Text, out _dec))
            {
                txtBottom.Focus();
                MessageBox.Show(UpdateUI.GetString("cantrenkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!decimal.TryParse(txtTop.Text, out _dec))
            {
                txtTop.Focus();
                MessageBox.Show(UpdateUI.GetString("canduoikhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!decimal.TryParse(txtPercent.Text, out _dec))
            {
                txtPercent.Focus();
                MessageBox.Show(UpdateUI.GetString("mucphainopkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!_listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_TaxInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _TaxInfo = new TaxInfo();
            txtBottom.Text = "0";
            txtTop.Text = "0";
            txtPercent.Text = "0";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!checkValid())
            {
                isChange = true;
                return;
            }
            TaxClient wcfTax = Service.Tax();
            Form2Info();
            if (_TaxInfo.ID > 0)//update
            {
                if (!_listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfTax.Tax_Update(_TaxInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!_listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _TaxInfo.ID = wcfTax.Tax_Insert(_TaxInfo);
                if (_TaxInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmTaxInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F4:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
            }
        }

        private void frmTaxInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfTax.Close();
            this.DialogResult = dlgResult;
        }

        void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}